// pages/map/map.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    latitude:"",
    longitude:"",
    createLatitude:"",
    createLongitude:"",
    markers:[
      // {
      //   iconPath: "/images/qy.png",
      //   longitude:111.36011847766883,
      //   latitude:30.64434169642198,
      //   id: 1,
      //   width: 30,
      //   height: 30
      // },
      // {
      //   iconPath: "/images/xm.png",
      //   longitude:111.37011847766883,
      //   latitude:30.65434169642198,
      //   id: 2,
      //   width: 30,
      //   height: 30
      // }
    ],
    library:4,
    showView:false,
    keyword:""
  },

  //移动地图
  regionchange(e) {
    if(e.detail.centerLocation!=undefined){
      this.setData({
        createLatitude:e.detail.centerLocation.latitude,
        createLongitude:e.detail.centerLocation.longitude
      });
    }
    this.setData({showView:false});
  },
  
  //点击列表
  onClicList:function(e){
    var id = e.currentTarget.dataset.id;
    var params = {id:id};
    app.request('post', '/api/Expose/projectDetail', params).then(res => {
          this.setData(res.data.detail);
          var detail = res.data.detail;
          this.setData({
            latitude: detail.lat,
            longitude:detail.lng,
            keyword:""
          });

    }).catch(err => console.log(err));
    // this.setData({showView:!this.data.showView});
  },

  //点击地图标点
	markertap(e) {
    var id = e.detail.markerId;
    var params = {id:id};
    app.request('post', '/api/Expose/projectDetail', params).then(res => {
          this.setData(res.data.detail);
    }).catch(err => console.log(err));
    this.setData({showView:!this.data.showView});
  },
  
  hidenView(){
    this.setData({showView:false});
  },

  onClickDetail:function(){
    var id = this.data.id;
    var auto = this.data.auto;
    var library = this.data.library;
    if(auto==1){
      wx.navigateTo({ url: `/pages/project/project?id=${id}&libraryTab=${library}` })
    } 
  },

  openMap: function () {

    var latitude = Number(this.data.lat);
    var longitude = Number(this.data.lng);
    wx.openLocation({
      latitude: latitude, // 纬度，范围为-90~90，负数表示南纬
      longitude: longitude, // 经度，范围为-180~180，负数表示西经
      scale: 28, // 缩放比例
      name:this.data.title,
      address:this.data.title
    })

      // wx.getLocation({
      //   type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      //     success: function (res) {
      //     // success
      //     console.log(relatitude);
      //     console.log(res.longitude);
          
      //   }
      // })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.projectList();
  },


  //查询企业
  projectList:function(){
    var keyword = this.data.keyword;
    var params = {keyword:keyword};
    app.request('post', '/api/Expose/projectList', params).then(res => {
          this.setData({
            markers:res.data.list
          });
    }).catch(err => console.log(err));
  },

  //搜索操作
  searachApi:function(e){
    var keyword = e.detail.value;
    this.setData({keyword:keyword});
    this.projectList();
  },

  //清空操作
  searachRemove:function(){
    this.setData({keyword:""});
    this.projectList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      this.createLocation();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.createLocation();
    this.projectList();
  },

  createLocation:function(){

    this.mapCtx = wx.createMapContext('myMap')
	  this.mapCtx.moveToLocation()
	  const lat= "markers[0].latitude";
	  const log= "markers[0].longitude";
    console.log(lat);
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success (res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy
        that.setData({
          latitude:latitude,longitude:longitude
        });
      }
     })
  },
  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})